package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.hubery.domain.Market;
import xyz.hubery.domain.TurnoverOrder;
import xyz.hubery.dto.CoinDTO;
import xyz.hubery.feign.CoinServiceFeign;
import xyz.hubery.service.MarketService;
import xyz.hubery.mapper.MarketMapper;
import org.springframework.stereotype.Service;
import xyz.hubery.service.TurnoverOrderService;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author huber
* @description 针对表【market(交易对配置信息)】的数据库操作Service实现
* @createDate 2025-10-16 07:45:08
*/
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market>
    implements MarketService{

    @Autowired
    private CoinServiceFeign coinServiceFeign;

    @Autowired
    private TurnoverOrderService turnoverOrderService;

    @Override
    public Market getMarketBySymbol(String symbol) {
        return getOne(new LambdaQueryWrapper<Market>()
                .eq(Market::getSymbol,symbol)
                .eq(Market::getStatus,1)
        );
    }

    /**
     * 查询某个交易区域下的所有市场
     * @param id 交易区域ID
     * @return
     */
    @Override
    public List<Market> getMarketsByTradeAreaId(Long id) {
        return list(new LambdaQueryWrapper<Market>()
                .eq(Market::getTradeAreaId,id)
                .eq(Market::getStatus,1)
                .orderByAsc(Market::getSort)
        );
    }

    @Override
    public Page<Market> findPage(Page<Market> page, Long tradeAreaId, Byte status) {
        return page(page,new LambdaQueryWrapper<Market>()
                .eq(tradeAreaId != null, Market::getTradeAreaId, tradeAreaId)
                .eq(status != null, Market::getStatus, status)
                .orderByAsc(Market::getSort)
        );
    }


    @Override
    public boolean save(Market entity) {
        Long buyCoinId = entity.getBuyCoinId(); // 买币种ID
        Long sellCoinId = entity.getSellCoinId(); // 卖币种ID
        List<CoinDTO> coinDTOS = coinServiceFeign.list(Arrays.asList(buyCoinId, sellCoinId));

        if(coinDTOS == null || coinDTOS.size() < 2) {
            throw new IllegalArgumentException("币种信息有误");
        }

        CoinDTO buyCoin = coinDTOS.stream().filter(c -> c.getId().equals(buyCoinId)).findFirst().orElse(null);
        CoinDTO sellCoin = coinDTOS.stream().filter(c -> c.getId().equals(sellCoinId)).findFirst().orElse(null);

        assert sellCoin != null;
        assert buyCoin != null;
        entity.setName(buyCoin.getName() + "/" + sellCoin.getName()); // 市场名称  基础货币 + 报价货币
        entity.setTitle(buyCoin.getTitle() + "/" + sellCoin.getTitle()); // 市场标题
        entity.setSymbol(buyCoin.getName() + sellCoin.getName()); // 市场标识符
        entity.setImg(sellCoin.getImg()); // 市场图标

        return super.save(entity);
    }

    /**
     * 根据交易区域id查询交易市场
     * @param id 交易区域id
     * @return
     */
    @Override
    public List<Market> getMarketsByAreaId(Long id) {
       return list(new LambdaQueryWrapper<Market>().eq(Market::getStatus,1).eq(Market::getTradeAreaId,id));
    }

    @Override
    public List<TurnoverOrder> listTradesById(Long id) {
      return   turnoverOrderService.list(new LambdaQueryWrapper<TurnoverOrder>()
              .eq(TurnoverOrder::getMarketId,id)
              .orderByDesc(TurnoverOrder::getCreated)
              .last("limit 100")
      );
    }
}




